//
//  MyCollectListViewController.m
//  InviteJob
//
//  Created by chirui on 2017/11/13.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "MyCollectListViewController.h"
#import "MainTableViewCell.h"
#import "WorkDetailsViewController.h"

@interface MyCollectListViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>{

    UITableView * _tableView;
    
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
    
//    UIView * noBodyView;
}

@end

@implementation MyCollectListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TitleView(@"我的收藏");
    _page = 1;
    _dataArray = @[].mutableCopy;
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightItemWithIcon:@"icon_news_delete"  target:self action:@selector(deledateBtn)];
    
    
    [self getMessageListData];
    [self creatTableView];
    
//     noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(WIDTH/2-100, (HEIGHT-64)/2-150, 200, 200) andMessageStr:@"没有相关消息！" onView:nil];
    
}

-(void)deledateBtn{
    
    if (_dataArray.count>0) {
        
        UIAlertView *alerte = [[UIAlertView alloc]initWithTitle:nil message:@"确定要清空消息列表吗？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil];
        alerte.tag = 1000;
        [alerte show];
    }else {
        
        [SHJudgeTools MBProgressHUDPromptWith:@"您还没有相关信息!" On:self.view];
        return;
        
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (alertView.tag == 1000) {
        if (buttonIndex == 0) {
            
            [self deledateCollectFor:@"" andType:@"all"];
        }
    }
}


-(void)creatTableView{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource =self;
    _tableView.delegate =self;
     _tableView.backgroundColor = strColor(@"f6f6f6");
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_tableView registerNib:[UINib nibWithNibName:[MainTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[MainTableViewCell reuseIdentifier]];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        [self getMessageListData];
        
    }];
    _tableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _page ++;
        [self getMessageListData];
        
    }];
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    
    if (_dataArray.count>10) {
        _tableView.mj_footer.hidden = NO;
    }else{
        _tableView.mj_footer.hidden = YES;
    }
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 130;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.01;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //左滑删除
    return UITableViewCellEditingStyleDelete;
    
}
//修改左滑删除按钮的title
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        _page = 1;
        //
        NSDictionary * dic = _dataArray[indexPath.row];
        [self deledateCollectFor:dic[@"f_id"] andType:@"part"];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    MainTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[MainTableViewCell reuseIdentifier] forIndexPath:indexPath];
    cell.workName.text = dic[@"cate_name"];
    cell.details.text = [NSString stringWithFormat:@"%@%@  %@  %@",dic[@"city_name"],dic[@"county_name"],dic[@"work_experience"],dic[@"education"]];
    cell.priceLabel.text = dic[@"salary"];
    cell.timeLabel.text = dic[@"update_time"];
    cell.companyName.text = dic[@"company_name"];
    cell.peopleNumber.text = dic[@"staff_number"];
    cell.tagLabel.text = dic[@"com_type"];
    cell.companyName.numberOfLines = 1;
    [cell.companyName sizeToFit];
    
    cell.tagLabel.frame = CGRectMake(CGRectGetMaxX(cell.companyName.frame)+5, CGRectGetMinY(cell.companyName.frame)+2, 30, 15);
    
    [cell.companyImage sd_setImageWithURL:ImageUrl(dic[@"company_logo"]) placeholderImage:ImageName(@"")];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = _dataArray[indexPath.row];
    WorkDetailsViewController * work =[[WorkDetailsViewController alloc]init];
    work.hidesBottomBarWhenPushed = YES;
    work.workIdStr=dic[@"zp_id"];
    [self.navigationController pushViewController:work animated:YES];
}

-(void)getMessageListData{
    
    isUser
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{
                           @"user_id":user.user_id,
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"perpage":@"10",
                           @"token":user.token
                           };
    
    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(myCollect_list_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            _totailPage = [responseObject[@"datas"][@"total_page"]  intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
                    [self hideNoDataView];
//                    noBodyView.hidden = YES;
                    if (_dataArray.count<10) {
                        
                        _tableView.mj_footer.hidden = YES;
                    }else {
                        _tableView.mj_footer.hidden = NO;
                    }
                }else {
                    [self shopNoDataViewBackgroundColor:[UIColor clearColor] withImage:@"img_home_no_news" withLabelMsg:@"没有任何数据" withButton:@"" isY:0];
//                    noBodyView.hidden = NO;
//                    [self.view addSubview:noBodyView];
                    _tableView.mj_footer.hidden = YES;
                    
                }
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];
                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
}

-(void)deledateCollectFor:(NSString *)messageId andType:(NSString *)typeStr{

    isUser
    NSDictionary * dic = @{
                           @"user_id":user.user_id,
                           @"f_id":messageId,
                           @"type":typeStr,
                           @"token":user.token
                           };
    
    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(deledate_collect_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
       
            SHuiMBhudTextTimeStr(@"删除成功");

            _page =1;
            [self getMessageListData];
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];




}


@end
